<template>
  <a-modal :width="1200" v-model:visible="showDetail" :footer="false" title="申购单详情" title-align="start" :mask-closable="false" @cancel="closedDialog">
    <div class="purchase-application-detail">
      <div class="header">
        <p class="title">基础信息</p>
      </div>
      <a-row class="detail-item">
        <a-col :span="12">
          <div class="flex">
            <p class="label">申购单号：</p>
            <div class="flex-c">
              <span class="highlight">{{ detailInfo.billNo }}</span>
              <CopyText style="margin-left: 5px" :val="detailInfo.billNo" />
            </div>
          </div>
        </a-col>
        <a-col :span="12">
          <div class="flex">
            <p class="label">类型：</p>
            <p class="val">{{ detailInfo.productType == 1 ? '普货' : '打样' }}</p>
          </div>
        </a-col>
      </a-row>

      <a-row class="detail-item">
        <a-col :span="12">
          <div class="flex">
            <p class="label">申购部门：</p>
            <p class="val">{{ detailInfo.salesmanDeptName }}</p>
          </div>
        </a-col>
        <a-col :span="12">
          <div class="flex">
            <p class="label">采购方：</p>
            <p class="val">{{ detailInfo.purchaseOrgName }}</p>
          </div>
        </a-col>
      </a-row>
      <a-row class="detail-item">
        <a-col :span="12">
          <div class="flex">
            <p class="label">申购人：</p>
            <p class="val">{{ detailInfo.salesmanName }}</p>
          </div>
        </a-col>
        <a-col :span="12">
          <div class="flex">
            <p class="label">收货仓：</p>
            <p class="val">{{ detailInfo.warehouseName }}（{{ detailInfo.warehouseCode }}）</p>
          </div>
        </a-col>
      </a-row>
      <a-row class="detail-item">
        <a-col :span="12">
          <div class="flex">
            <span class="label" style="flex-shrink: 0">附件：</span>
            <ul style="margin-right: 10px" v-if="detailInfo.attachment">
              <li v-for="(url, i) in detailInfo.attachment.split(',')" style="margin-bottom: 4px" :key="i">
                <a-tag color="arcoblue" class="cursor" @click="downloadFile(url)">
                  <span>{{ url }}</span>
                  <icon-to-bottom />
                </a-tag>
              </li>
            </ul>
            <p v-else>--</p>
          </div>
        </a-col>
        <a-col :span="12">
          <div class="flex">
            <span class="label">备注：</span>
            <span style="flex: 1">{{ detailInfo.remark }}</span>
          </div>
        </a-col>
      </a-row>
      <a-row class="detail-item">
        <a-col :span="12">
          <div class="flex">
            <p class="label">期望交货日期：</p>
            <p class="val">{{ $setTime(detailInfo.gmtExpectDelivery, 'YYYY-MM-DD') }}</p>
          </div>
        </a-col>
        <a-col :span="12">
          <div class="flex">
            <p class="label">创建时间：</p>
            <p class="val">{{ $setTime(detailInfo.gmtCreate) }}</p>
          </div>
        </a-col>
      </a-row>

      <div class="purchase-detail-order">
        <a-tabs style="margin-bottom: 10px" v-model:active-key="activeTab">
          <a-tab-pane key="1" title="申购明细" />
          <a-tab-pane key="2" title="操作日志" />
        </a-tabs>
        <div style="flex: 1; overflow: hidden">
          <a-table v-show="activeTab == '1'" class="table-two-line show-border-table" ref="tableRef" :data="detailInfo.billItems" :scroll="{ y: '100%' }" size="mini" :pagination="false">
            <template #columns>
              <a-table-column data-index="img" title="图片" :width="220" fixed="left">
                <template #cell="{ record }">
                  <div class="flex-c">
                    <ImgPopover :src="record.productImg">
                      <template #con>
                        <a-image width="50" height="50" fit="contain" :src="record.productImg" />
                      </template>
                    </ImgPopover>
                    <div class="product-info">
                      <p>{{ record.sku }}</p>
                      <p>{{ getProductSpecs(record) }}</p>
                      <span class="urgent">急</span>
                    </div>
                  </div>
                </template>
              </a-table-column>
              <a-table-column data-index="space" title="采购参数" :width="150">
                <template #cell="{ record }">
                  <PurchasePropPopover v-if="record.purchaseSpecsArr" :purchaseSpecsArr="record.purchaseSpecsArr" />
                </template>
              </a-table-column>
              <a-table-column data-index="remark" title="备注" :width="100" />
              <a-table-column data-index="shopName" title="店铺" :width="100" />
              <a-table-column data-index="planAmt" title="申购数量" :width="70" />
              <a-table-column data-index="warehouseRealAmt" title="已入库数" :width="70" />
              <a-table-column data-index="cutoffAmt" title="已截单数" :width="70" />
              <a-table-column data-index="laborPrice" title="工费单价" :width="70">
                <template #cell="{ record }">
                  <p>{{ record.laborPriceType == 1 ? record.laborPrice : Number(record.laborRate || 0) * Number(record.productPrice || 0) }}</p>
                </template>
              </a-table-column>
              <a-table-column data-index="productPrice" title="产品单价" :width="70" />
              <a-table-column data-index="materialPrice" title="材质单价" :width="70" />
              <a-table-column data-index="标签" title="标签" :width="140">
                <template #cell="{ record }">
                  <PurchaseLabelsPopover :labels="record.labels" source="application" :edit="false" :billInfo="record" :showNum="3" />
                </template>
              </a-table-column>
            </template>
          </a-table>
          <PurchaseLogs v-if="activeTab == '2'" :currentData="detailInfo" api="findPurchasePlanLogPageV2" />
        </div>
      </div>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import PurchaseLogs from '@/views/purchaseApplication/components/purchaseLogs.vue'
  import { getPurchasePlanInfo } from '@/api/purchaseApplication'
  import { parseJson } from '@/utils'
  import PurchasePropPopover from './purchasePropPopover.vue'
  import PurchaseLabelsPopover from './purchaseLabelsPopover.vue'
  const emit = defineEmits(['closed'])
  const props = defineProps({
    isShow: {
      type: Boolean,
      default: false,
    },
    source: {
      type: String,
      default: 'demander',
    },
    currentId: {
      type: String,
    },
  })

  const showDetail = ref(props.isShow)
  const detailInfo = ref<any>({})
  const activeTab = ref('1')

  // 关闭
  function closedDialog() {
    showDetail.value = false
    emit('closed')
  }

  // 获取详情
  async function getDetail() {
    if (!props.currentId) return
    const res: any = await getPurchasePlanInfo({ id: props.currentId })
    detailInfo.value = res?.data || {}
    detailInfo.value.billItems.forEach((item: any) => {
      let purchaseSpecs = item.purchaseSpecs ? parseJson(item.purchaseSpecs, {}) : {}
      item.purchaseSpecsArr = Object.values(purchaseSpecs)
    })
  }
  getDetail()
  //  下载
  function downloadFile(url: string) {
    let _url = url
    if (_url.indexOf('?') > -1) {
      _url += '&response-content-type=application/octet-stream'
    } else {
      _url += '?response-content-type=application/octet-stream'
    }
    let link = document.createElement('a')
    link.href = _url
    link.click()
  }
  // 显示sku规格
  function getProductSpecs(item: any) {
    let productSpecs = item.productSpecs ? parseJson(item.productSpecs, []) : []
    return productSpecs.map((el: any) => el.valueCn)?.join('*') || '--'
  }
</script>

<style lang="less" scoped>
  .purchase-application-detail {
    height: calc(84vh - 100px);
    display: flex;
    flex-direction: column;
    .header {
      font-size: 14px;
      font-weight: bold;
      margin-bottom: 10px;
      border-left: 3px solid rgba(var(--primary-6), 0.5);
      padding-left: 10px;
      .title {
        margin-right: 30px;
        width: 90px;
      }
    }
    .purchase-detail-order {
      margin-top: 20px;
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      .arco-tab {
        margin-top: 10px;
      }
    }
    .detail-item {
      margin-bottom: 5px;
      .label {
        width: 120px;
        text-align: right;
      }
      .val {
        color: #666;
      }
      .info {
        font-size: 12px;
        color: #999;
      }
    }
    .product-info {
      margin-left: 5px;
    }
    .urgent {
      font-size: 12px;
      display: inline-block;
      background-color: red;
      color: #fff;
      width: 18px;
      border-radius: 50%;
      line-height: 18px;
      text-align: center;
      transform: scale(0.82);
    }
  }
</style>
